package com.soft.admin.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.hibernate.criterion.Order;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.soft.admin.common.ElementConst;
import com.soft.admin.dto.AssistDTO;
import com.soft.admin.service.AssistService;
import com.soft.dao.AssistDao;
import com.soft.model.Assist;
import com.soft.util.DateUtil;
import com.soft.util.FileUtil;
import com.soft.util.JsonUtil;

@SuppressWarnings("unchecked")
@Component
public class AssistServiceImpl implements AssistService {
	private AssistDao assistDao;

	@Autowired
	public void setAssistDao(AssistDao assistDao) {
		this.assistDao = assistDao;
	}
	
	@Transactional(readOnly = true)
	public String get() {
		List<Assist> assistLst = this.assistDao.findAll(Assist.class,  new Order[] { Order.asc("id") });
		List<AssistDTO> dtos = new ArrayList<AssistDTO>();
		for (Assist assist : assistLst) {
			AssistDTO dto = new AssistDTO();
			BeanUtils.copyProperties(assist, dto, new String[]{"updateDate"});
			dto.setUpdateDate(DateUtil.format(assist.getUpdateDate()));
			dtos.add(dto);
		}
		return JsonUtil.toString(dtos);
	}

	@Transactional
	public String addOrEdit(HttpServletRequest request, AssistDTO params) {
		Assist assist  = (Assist)this.assistDao.get(Assist.class, params.getId());
		MultipartHttpServletRequest mulrequest = (MultipartHttpServletRequest)request;
		CommonsMultipartFile file = (CommonsMultipartFile)mulrequest.getFile("filepic");
		if (file.getSize() > 0) {
			DiskFileItem fileItem = (DiskFileItem) file.getFileItem();
			String fileStr = fileItem.getName().substring(fileItem.getName().lastIndexOf(".")).toLowerCase();
			if (!".jpg".equals(fileStr) 
					&& !".png".equals(fileStr) 
					&& !".bmp".equals(fileStr) 
					&& !".gif".equals(fileStr)) {
				return JsonUtil.toRes("保存失败！", "图片格式错误");
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + fileStr;
			String rootPath = request.getSession().getServletContext().getRealPath(ElementConst.IMGSPATH);
			String path = rootPath + File.separator + FileUtil.getPath(fileName) + File.separator;
			File targetDir = new File(path);
			if (!targetDir.exists()) targetDir.mkdirs();
			try {
				FileCopyUtils.copy(file.getBytes(), new File(path + fileName));
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (assist.getTitleImg() != null && !"".equals(assist.getTitleImg())) {
				FileUtil.delete(rootPath + File.separator + assist.getTitleImg());
			}
			assist.setTitleImg(FileUtil.getPath(fileName) + "/" + fileName);
		}
		assist.setUpdateDate(new Date());
		assist.setTitle(params.getTitle());
		assist.setTxt(params.getTxt());
		this.assistDao.addOrEdit(assist);
		return JsonUtil.toRes("保存成功！");
	}
}
